<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="c"  uri="http://java.sun.com/jsp/jstl/core" %>
<%@taglib prefix="shiro" uri="http://shiro.apache.org/tags" %>
<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8">
	<title>${SYSTEM_NAME}</title>
	<meta name="renderer" content="webkit">
	<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
	<meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
	<link rel="stylesheet" href="${pageContext.request.contextPath}/static/layuiadmin/layui/css/layui.css?v=${FILE_VERSION}" media="all">
	<link rel="stylesheet" href="${pageContext.request.contextPath}/static/layuiadmin/style/admin.css?v=${FILE_VERSION}" media="all">
	<link rel="stylesheet" href="${pageContext.request.contextPath}/static/js/zTree_v3/css/metroStyle/metroStyle.css?v=${FILE_VERSION}" type="text/css">
</head>
<body>
<input type="hidden" id="orgId" value="${orgId}">
<div class="layui-fluid">
	<div class="layui-card">
		<div class="layui-card-header">用户管理</div>
		<div class="layui-card-body">
			<div class="layui-row layui-col-space10">
				<div class="layui-col-xs2">
					<div style="border: 1px solid #ccc;max-height: 550px;min-height:400px; overflow: auto;" >
						<ul id="treeRegion" class="ztree">
							机构加载中......
						</ul>
					</div>
				</div>

				<div class="layui-col-xs10" style="padding-left: 15px">
					<div class="layui-form layer-query">
						<div class="layui-form-item">
							<div class="layui-inline">
								<label class="layui-form-label">登录账号：</label>
								<div class="layui-input-inline">
									<input type="text" name="username" placeholder="请输入登录账号" id="username" autocomplete="off" class="layui-input">
								</div>
							</div>

							<div class="layui-inline">
								<label class="layui-form-label">真实姓名：</label>
								<div class="layui-input-inline">
									<input type="text" name="username" placeholder="请输入真实姓名"  id="realName" autocomplete="off" class="layui-input">
								</div>
							</div>
							<div class="layui-inline">
								<button class="layui-btn layui-btn-primary layui-border-green" onclick="reload()">查询</button>
							</div>
						</div>
					</div>
					<hr class="layui-bg-green">
					<table class="layui-table" lay-filter="tableList" id="layerTable">
					</table>
				</div>
			</div>
		</div>
	</div>
</div>

<script type="text/html" id="toolbar">
	<div class="layui-btn-container">
		<button class="layui-btn layui-btn-primary layui-border-blue" onclick="addLayerLocal();">新增</button>
		<button class="layui-btn layui-btn-primary layui-border-blue" onclick="modiLocal();">编辑</button>
		<button class="layui-btn layui-btn-primary layui-border-red" onclick="deleteBatchLocal();">删除</button>
		<button class="layui-btn layui-btn-primary layui-border-blue" onclick="init()">更新缓存</button>
	</div>
</script>

<script type="text/html" id="roles">
	{{# for(var i = 0;i< d.roleList.length;i++){ }}
	<a class="layui-btn layui-btn-radius layui-btn-xs  layui-btn-warm"> {{ d.roleList[i].roleName }}</a>
	{{#}}}
</script>

<script type="text/html" id="barDemo">
	{{#if(d.id != '<shiro:principal property="id"/>'){ }}
	<a class="layui-btn layui-btn-primary layui-border-green layui-btn-xs" lay-event="setRole">设置角色</a>
	<a class="layui-btn layui-btn-primary layui-border-blue layui-btn-xs" lay-event="edit">编辑</a>
	<a class="layui-btn layui-btn-primary layui-border-red layui-btn-xs " lay-event="del">删除</a>
	{{# } }}
	<a class="layui-btn layui-btn-primary layui-border-black layui-btn-xs" lay-event="initPwd">初始化密码</a>
</script>

'}
<script type="text/html" id="disableTpl">
	<input type="checkbox" name="disable" lay-skin="switch" lay-text="禁用|解禁" lay-filter="disableFilter"
		   value="{{ d.disable }}"  data-json="{{ encodeURIComponent(JSON.stringify(d)) }}" {{ d.disable == '1' ? 'checked' : '' }}>
</script>

<script type="text/html" id="lockStatusTpl">
	<input type="checkbox" name="lockStatus" lay-skin="switch" lay-text="解锁|锁定" lay-filter="lockStatusFilter"
		   value="{{ d.lockStatus }}"  data-json="{{ encodeURIComponent(JSON.stringify(d)) }}" {{ d.lockStatus == '1' ? 'checked' : '' }}>
</script>

<script src="${pageContext.request.contextPath}/static/js/common-2.0.0.js?v=${FILE_VERSION}"></script>
<script src="${pageContext.request.contextPath}/static/layuiadmin/layui/layui.js?v=${FILE_VERSION}"></script>
<script src="${pageContext.request.contextPath}/static/js/jquery-3.3.1.min.js?v=${FILE_VERSION}"></script>
<script src="${pageContext.request.contextPath}/static/js/zTree_v3/js/jquery.ztree.core.js?v=${FILE_VERSION}"></script>
<script src="${pageContext.request.contextPath}/static/js/common/commOrgTree.js?v=${FILE_VERSION}"></script>
<script src="${pageContext.request.contextPath}/static/js/common/core-2.0.0.js?v=${FILE_VERSION}"></script>

<script src="${pageContext.request.contextPath}/static/js/core-2.0.0.js?v=${FILE_VERSION}"></script>
<script>
	var table;
	layui.use(['table', 'form','layer'], function() {
		createOrgTree(true,"/sys/org/list/listAllTree",false);
		var form = layui.form;
		var layer = layui.layer;
		table = layui.table;
		table.render({ //其它参数在此省略
			elem : '#layerTable',
			even :"true",
			toolbar: '#toolbar',
			defaultToolbar: ['filter', 'exports', 'print'],
			limit: 10,
			limits: [10,30,50,100],
			id: 'tableList',
			url: getContextPath() + '/sys/user/list/listInfo',
			page:true,
			method: 'post', //如果无需自定义HTTP类型，可不加该参数
			cols:  [[ //标题栏
				{checkbox: true, fixed: true}
				,{field:'orgName', minWidth:150,title:"所属机构"}
				,{field:'username', title:"登录账号",minWidth: 130}
				,{field:'realName', width:110,title:"真实姓名"}
				,{field:'phone', width:120,title:"手机号"}
				,{field:'typeName', minWidth:140,title:"权限范围"}
				,{field:'disable', width:100,title:"是否禁用",templet: '#disableTpl'}
				,{field:'lockStatus', width:100,title:"是否锁定",templet: '#lockStatusTpl'}
				,{minWidth:120,title:"已设置角色",templet: '#roles'}
				,{fixed: 'right', width:290, align:'center',title:"操作", toolbar: '#barDemo'}
			]],
			request: {
				pageName: 'current' //页码的参数名称，默认：page
				,limitName: 'size' //每页数据量的参数名，默认：limit
			}
		});

		//监听工具条
		table.on('tool(tableList)', function(obj){
			var data = obj.data;
			if(obj.event === 'initPwd'){  //初始化密码
				initPwd(data);
			} else if(obj.event === 'del'){
				deleteById(data,obj,'/sys/user/del/deleteById');
			} else if(obj.event === 'edit'){
				addLayer('/sys/user/edit/input', data.id);
			} else if(obj.event === 'setRole'){  //设置角色
				setRole(data.id);
			}
		});

		//监听操作
		form.on('switch(disableFilter)', function(obj){
			var dataJson = JSON.parse(decodeURIComponent($(this).data('json')));
			var id = dataJson.id;
			var disable = obj.elem.checked ? "1" : "0";
			$ajax({
				url: '/sys/user/updateDisable',
				isShowLayer:false,
				data :  {
					"id" : id,
					"disable" : disable
				},
				succCallback: function (json) {
					if(json.code == 0){
						dataJson = table.clearCacheKey(dataJson);
						layer.msg(json.msg);
					}
				}
			});
		});

		//监听操作
		form.on('switch(lockStatusFilter)', function(obj){
			var dataJson = JSON.parse(decodeURIComponent($(this).data('json')));
			var id = dataJson.id;
			var lockStatus = obj.elem.checked ? "1" : "0";
			$ajax({
				url: '/sys/user/updateLockStatus',
				isShowLayer:false,
				data :  {
					"id" : id,
					"lockStatus" : lockStatus
				},
				succCallback: function (json) {
					if(json.code == 0){
						dataJson = table.clearCacheKey(dataJson);
						layer.msg(json.msg);
					}
				}
			});
		});
	});

	/**
	 * 树节点点击回调
	 * @param node
	 */
	function orgTreeClickCallback(node){
		reload(node.id);
	}

	function modiLocal(){
		var checkStatus = table.checkStatus('tableList')
				,data = checkStatus.data;
		if(data.length == 0 || data.length > 1){
			layer.msg("请选择一条数据");
			return false;
		}
		if(data[0].id == '<shiro:principal property="id"/>'){
			layer.msg("不允许编辑此管理员");
			return;
		}
		addLayer('/sys/user/edit/input',data[0].id);
	}

	function deleteBatchLocal(){
		var checkStatus = table.checkStatus('tableList')
				,data = checkStatus.data;
		if(data.length == 0 ){
			layer.msg("请至少选择一条数据");
			return false;
		}else{
			for (var i = 0; i < data.length; i++) {
				if(data[i].id == '<shiro:principal property="id"/>'){
					layer.msg("不允许删除初始化的管理员");
					return;
				}
			}
		}
		deleteBatch('/sys/user/del/deleteByIds');
	}

	function reload(orgId){
		if(typeof(orgId) != "undefined"){
			$('#orgId').val(orgId);
		}
		let param = {
			"realName":$("#realName").val(),
			"orgId": $('#orgId').val(),
			"username": $('#username').val()
		};
		coreReload("tableList",param);
	}

	function initPwd(data){
		layer.confirm('真的要初始化此用户密码吗？', function(index){
			$ajax({
				url: "/sys/user/add/initPwd",
				data: {id : data.id},
				succCallback: function (json) {
					//layer.msg(json.msg);
					layer.confirm(json.msg, {
						btn: ['确认']
					},function(idx){
						layer.close(idx);
					});
				}
			});
		});
	}

	function setRole(id){
		var url  = getContextPath() +  '/sys/user/add/setRole?userId=' + id ;
		layer.open({
			title : "设置角色",
			type: 2,
			area: ['500px', '400px'],
			anim: 2,
			content: [url, 'no']
		});
	}

	function addLayerLocal(){
		addLayer('/sys/user/add/input?orgId='+ $('#orgId').val());
	}

</script>
</body>
</html>